package com.syni.mdd.yhd.common.all.listen.userRece;

import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.entity.BmsBusiness;
import com.syni.mdd.yhd.common.all.entity.BmsDxGroupBuy;
import com.syni.mdd.yhd.common.all.entity.BmsDxGroupBuyUse;
import com.syni.mdd.yhd.common.all.entity.BmsUserBusinessAuth;
import com.syni.mdd.yhd.common.all.entity.activity.BmsMealPrize;
import com.syni.mdd.yhd.common.all.entity.coupon.BmsUserReceCoupon;
import com.syni.mdd.yhd.common.all.entity.user.BmsUser;
import com.syni.mdd.yhd.common.all.service.*;
import com.syni.mdd.yhd.common.all.service.activity.BmsMealPrizeService;
import com.syni.mdd.sjb.common.component.utils.common.utils.DateTimeUtils;
import com.syni.mdd.yhd.common.all.utils.SmsUtils;
import com.syni.mdd.yhd.common.all.utils.StringUtils;
import com.syni.mdd.yhd.common.all.utils.TokenManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @className OrderListener
 * @description TOO
 * @Author cfx
 * @DATE 2019/8/8 15:46
 * @VERSION 1.0
 **/
@Component
public class UserReceListener implements ApplicationListener<UserReceEvent> {
    private final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    BmsMealPrizeService bmsMealPrizeService;

    @Autowired
    BmsUserBusinessAuthService bmsUserBusinessAuthService;

    @Autowired
    BmsCommonService bmsCommonService;

    @Autowired
    BmsBusinessVerRecordService bmsBusinessVerRecordService;

    @Override
    @Async
    public void onApplicationEvent(UserReceEvent userReceEvent) {
        log.info("......................领取券核销...................");
        List<BmsUserReceCoupon> bmsUserReceCoupons = userReceEvent.getBmsUserReceCoupon();
        if(bmsUserReceCoupons != null && bmsUserReceCoupons.size() > 0) {
            for(BmsUserReceCoupon bmsUserReceCoupon : bmsUserReceCoupons) {
                //插入核销记录表
                bmsBusinessVerRecordService.addReceVerRecord(bmsUserReceCoupon);
                if (bmsUserReceCoupon.getType() == Constants.FLI_COUPON) {
                    //判断是否是霸王餐券
                    BmsMealPrize bmsMealPrize = bmsMealPrizeService.getBaseDao().findByBusinessCouponId(bmsUserReceCoupon.getBusinessCouponId());
                    if (bmsMealPrize != null) {
                        sendOverMealSms(bmsUserReceCoupon);
                    }
                }
            }
        }

    }
    /**
     * @Description 发送购买短信
     * @Author: cfx
     * @Date: 2019/8/28
     */
    public void sendOverMealSms(BmsUserReceCoupon bmsUserReceCoupon){
        final String userTemplateId = "groupBuyUse.user.over.meal.templateId";
        final String businessTemplateIds = "groupBuyUse.business.over.meal.templateId";
        BmsUser bmsUser = TokenManager.getUser(bmsUserReceCoupon.getUserId());
        Integer num = 1;
        BmsUserBusinessAuth bmsUserBusinessAuth = bmsUserBusinessAuthService.findByBusinessIdNotReject(bmsUserReceCoupon.getBusinessId());
        if(bmsUserBusinessAuth != null) {
            BmsBusiness bmsBusiness = bmsUserBusinessAuth.getBmsBusiness();
            //发送给商家
            Integer businessUserId = bmsUserBusinessAuth.getBmsUserId();
            BmsUser businessUser = TokenManager.getUser(businessUserId);
            if (bmsUser != null && StringUtils.isNotBlank(businessUser.getPhone())) {
                String phone = businessUser.getPhone();
                String code = String.format("「%s」",bmsUserReceCoupon.getConsumerCode());
                String couponName = bmsUserReceCoupon.getBmsBusinessCoupon().getCouponName();
                String[] params = {
                        DateTimeUtils.converDateToString(bmsUserReceCoupon.getUseTime(), DateTimeUtils.DATE_PATTERN_DAY_06),
                        couponName,
                        code};
                if(BmsCommonService.checkRunProfile(BmsCommonService.RUN_PRO)){
                    SmsUtils.send(phone,businessTemplateIds,params,"business");
                }
            }
            //发送给用户
            if (bmsUser != null && StringUtils.isNotBlank(bmsUser.getPhone())) {
                String phone = bmsUser.getPhone();
                String[] params = {
                        DateTimeUtils.converDateToString(bmsUserReceCoupon.getUseTime(), DateTimeUtils.DATE_PATTERN_DAY_06),
                        num.toString(),
                        bmsBusiness.getVendorName(),
                        bmsUserReceCoupon.getBmsBusinessCoupon().getCouponName()
                        };
                if(BmsCommonService.checkRunProfile(BmsCommonService.RUN_PRO)){
                    SmsUtils.send(phone, userTemplateId, params);
                }
            }

        }
    }
}
